<?php
namespace Home\Biz;
class CommonBiz {
	/**
	 * 获取不重复唯一键
	 */
	public static function getNewSeq($type) {
		M ( 'seq', '', 'DB_system' )->execute ( "INSERT INTO `seq` (`name`) VALUES ('$type')" );
		$seq = M ( 'seq', '', 'DB_system' )->max ( 'id' );
		$seq = ( int ) $seq;
		return $seq;
	}
	
	
	public static function getAlipayBatchNo(){
		$seq = CommonBiz::getNewSeq('batchNo');
		$seq = 10000000 + $seq;
		return strval($seq);
	}
	
	
	public static function buildRoleAuth($lst_rules) { 
		
		$menus = array();
		 
		foreach ($lst_rules as $rule_sigle){
			$menu_id = $rule_sigle['menu_id'];
			$menu = M('menu_resource','','DB_system')->where("menu_id = '%s'", $menu_id)->find();
			if($menu['menu_display']==1){
				array_push($menus,$menu);
			}
		}
		 
		$lst_menu1_en = array();
		 
		foreach($menus as $menu){
			$parent_name = $menu['parent_name'];
			$parent_id = $menu['parent_id'];
			 
			$f_lst_menu1_en = array_filter($lst_menu1_en,
					function($item) use ($parent_id){
						 
						return $parent_id == $item['parent_id'];
						 
					});
			 
			if(empty($f_lst_menu1_en)){
				$menu2_en = array(
						'menu_name'	=> $menu['menu_name'],
						'menu_url'	=> "/".$menu['menu_url'],
				);
				$lst_menu2_en = array();
				array_push($lst_menu2_en, $menu2_en);
				 
				$new_menu1_en = array(
						'parent_id' => $parent_id,
						'parent_name' => $parent_name,
						'lst_menu2'		=> $lst_menu2_en,
				);
				array_push($lst_menu1_en, $new_menu1_en);
			}
			else{
				 
				$menu2_en = array(
						'menu_name'	=> $menu['menu_name'],
						'menu_url'	=> "/".$menu['menu_url'],
				);
				 
				$curr_menu1_en 		= each($f_lst_menu1_en);
				$curr_menu1_en_key 	= $curr_menu1_en['key'];
				array_push($lst_menu1_en[$curr_menu1_en_key]['lst_menu2'], $menu2_en);
			}
		
		}
		return $lst_menu1_en;
		
	}
	
	public static function checkRuleAuth($user_id,$local_domain,$request_url) {
		
		$menu = M('menu_resource','','DB_system')->where("menu_domain = '%s' and menu_url='%s' ", array($local_domain, $request_url))->find();

		$menu_id = $menu['menu_id'];
		
		$user_rule = M('user_rule','','DB_system')->where("user_name = '%s' and menu_id='%s' ", array($user_id, $menu_id))->find();

		if($user_rule !=null){
			return true;
		}
		
		return false;
	}
	
	
	public static function getGetParamsStr(){
		$get_str_array = array();
		$all_get_keys = array_keys($_GET);
	
		foreach($all_get_keys as $key){
			if(!is_numeric($key)){
				array_push($get_str_array, $key."=".$_GET[$key]);
			}
		}
		$get_str = implode('&', $get_str_array);
		return $get_str;
	}
	
	public static function getPostParamsStr(){
		$get_str_array = array();
		$all_get_keys = array_keys(I('post.'));
		foreach($all_get_keys as $key){
			if(!is_numeric($key)){
				array_push($get_str_array, $key."=".$_POST[$key]);
			}
		}
		$get_str = implode('&', $get_str_array);
		return $get_str;
	}
	
	
	public static function getParamsStr(){
		$str_array 	= array();
		$get_str 	= CommonBiz::getGetParamsStr();
		$post_str 	= CommonBiz::getPostParamsStr();
		array_push($str_array, $get_str);
		array_push($str_array, $post_str);
	
		$str 		= implode('&', $str_array);
		return trim($str, '&');
	}
	
	
	public static function getRequestParams(){
		return array_merge($_POST, $_GET);
	}
	
	public static function saveOperation($userId,$type,$domain,$moudle,$util,$paramsText)
	{
		//写入order_payment
		$user_operation_log_en = array(
				'user_id'		=> $userId,
				'type'	=> $type,
				'domain'=> $domain,
				'moudle'=> $moudle,
				'util'		=> $util,
				'param_text'=>$paramsText,
				'create_user'	=> 'UserOperationBiz',
				'create_date'	=> date("y-m-d H:i:s", time()),
		);
		M('user_operation_log','','DB_system')->add($user_operation_log_en);
	
	}
	
}